package com.edu.test;/*
 * @Author:liwentao
 * @Date:2025/10/24-10-24-9:30
 */

import com.edu.entity.Department;
import com.edu.entity.Employee;
import com.edu.mapper.DepartmentMapper;
import com.edu.mapper.EmployeeMapper;
import com.edu.mapper.PostMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

public class test01 {
    public static SqlSession getSession() throws IOException {
        String path = "mybatis-config.xml";
        InputStream ins = Resources.getResourceAsStream(path);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
        SqlSession session = sqlSessionFactory.openSession();
        return session;
    }
    static SqlSession session;
    static {
        try {
            session = getSession();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    static EmployeeMapper em = session.getMapper(EmployeeMapper.class);
    static DepartmentMapper dm = session.getMapper(DepartmentMapper.class);
    static PostMapper pm = session.getMapper(PostMapper.class);

    public static void main(String[] args) throws ParseException {
        System.out.println("test01");
        Employee emp1 = em.selectByPrimaryKey(1);
        System.out.println(emp1);

        System.out.println("test02");
        Employee newEmp1 = new Employee();
        newEmp1.setEmpName("王五1");
        Date date1 = new Date();
        newEmp1.setEmpDate(date1);
        newEmp1.setEmpMgr(3);
        newEmp1.setEmpDid(1);
        newEmp1.setEmpPid(1);
        newEmp1.setEmpSalary(BigDecimal.valueOf(1345));
        em.insert(newEmp1);

        System.out.println("test03");
        em.deleteByPrimaryKey(1);

        System.out.println("test04");
        List<Employee> list1 =  em.sqlFindAllEmpByLimit(1,20);
        for (Employee employee : list1) {
            System.out.println(employee);
        }

       // System.out.println("test05");
        //System.out.println(dm.sqlFindEmpByDepName("a部"));

        System.out.println("test06");
        List<Department> list2 = dm.sqlFindAllDepAndEmp();
        for (Department department : list2) {
            System.out.println(department);
        }

        System.out.println("test07");
        List<Employee> list3 = em.sqlFindAllEmpAndTopEmp();
        for (Employee employee : list3) {
            System.out.println(employee);
        }

        System.out.println("test08");
        List<Department> list4 = dm.sqlFindDepByDepId(1);
        for (Department department : list4) {
            System.out.println(department);
        }

        System.out.println("test09");
        System.out.println(pm.sqlFindPostByName("aaaa"));
    }
}
